'''
Created on Aug 26, 2010

@author: dexen
'''
import os 
import sys
import random

#INITIALISATION 
#function for generating genotype
def random_genotype(size):
    genotype = []
    for _ in range(size):
        random.seed()
        genotype.append(random.random())
    return genotype

#DEVELOPMENT
def enableHouModule():
    '''Set up the environment so that "import hou" works.'''
    # Importing hou will load in Houdini's libraries and initialize Houdini.
    # In turn, Houdini will load any HDK extensions written in C++.  These
    # extensions need to link against Houdini's libraries, so we need to
    # make sure that the symbols from Houdini's libraries are visible to
    # other libraries that Houdini loads.  So, we adjust Python's dlopen
    # flags before importing hou.
    if hasattr(sys, "setdlopenflags"):
        old_dlopen_flags = sys.getdlopenflags()
        import DLFCN
        sys.setdlopenflags(old_dlopen_flags | DLFCN.RTLD_GLOBAL)

    try:
        import hou
    except ImportError:
        # Add $HFS/houdini/python2.5libs to sys.path so Python can find the
        # hou module.
        sys.path.append( os.environ['HFS'] + "/houdini/python%d.%dlibs" % sys.version_info[:2])
        import hou
    finally:
        if hasattr(sys, "setdlopenflags"):
            sys.setdlopenflags(old_dlopen_flags)
#houdini geometry
def houdini(genotypes, basefilepath, count):
    enableHouModule()
    import hou
    hou.hipFile.load(basefilepath)
    genotype = hou.node("/obj/geo1/genotypes")
    for cnt in range(len(genotypes)):
        genotype.setParms({"genotype"+str(cnt+1):genotypes[cnt]})
    hou.node("/obj/geo1/file2").geometry().saveToFile("houdinibasefile/geometry/houseschema" + count + ".bgeo")
    hou.hipFile.save()
    
def houdini_overall(basefilepath, genotypes):
    enableHouModule()
    import hou
    hou.hipFile.load(basefilepath)
    genotype = hou.node("/obj/geo1/genotypes")
    genotype.setParms({"genotype" : genotypes[0]})
    hou.setFrame(500)
    hou.node("/obj/geo1/merge1").geometry().saveToFile("houdinibasefile/layout" +".bgeo")
    
for count in range(15):
    genotypes = random_genotype(68)
    basefilepath = "/home/dexen/workspace/houseschema/houseschema/houdinibasefile/houseschema.hipnc"
    count = str(count)
    houdini(genotypes, basefilepath, count)

scatter_genotype = random_genotype(1)
layout_file_path = "/home/dexen/workspace/houseschema/houseschema/houdinibasefile/voronoi.hipnc"
houdini_overall(layout_file_path, scatter_genotype)
